package cn.sdormitory.controller.exam;


import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.exam.service.ExamResultInfoService;
import cn.sdormitory.exam.vo.ExamInfoVO;
import cn.sdormitory.exam.vo.ExamResultInfoVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "exam => 考试结果")
@RequestMapping("/exam/result")
public class ExamResultInfoController {

    @Resource
    private ExamResultInfoService examResultInfoService;

    @ApiOperation("分页查询列表")
    @PostMapping("/pageList")
//    @PreAuthorize("@ss.hasPermi('exam:info:list')")
    public CommonResult<CommonPage<ExamResultInfoVO>> findExamResultPageList(@RequestBody ExamResultInfoVO vo){
        IPage<ExamResultInfoVO> pageList = examResultInfoService.findExamResultPageList(vo);
        return CommonResult.success(CommonPage.restPage(pageList));
    }

    @ApiOperation("新增or修改考试结果")
    @PostMapping("/edit")
//    @PreAuthorize("@ss.hasPermi('exam:info:list')")
    public CommonResult<Integer> addOrUpdateExamResult(@RequestBody ExamResultInfoVO vo){
        if(vo.getExamStudents()==null || vo.getExamStudents().size()==0){
            return CommonResult.failed("缺少考试学生信息");
        }
        int num = examResultInfoService.addOrUpdateExamResult(vo);
        if(num == -1){
            return CommonResult.failed("考试成绩已经被录入，不能重复录入");
        }
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除考试结果")
    @PostMapping("/delete")
//    @PreAuthorize("@ss.hasPermi('exam:info:list')")
    public CommonResult<Integer> deleteResultById(@RequestBody ExamResultInfoVO vo){
        int num = examResultInfoService.deleteResultById(vo);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("批量删除考试结果")
    @PostMapping("/deleteBatch")
//    @PreAuthorize("@ss.hasPermi('exam:info:list')")
    public CommonResult<Integer> deleteBatchExamResult(@RequestBody List<ExamResultInfoVO> list){
        int num = examResultInfoService.deleteBatchExamResult(list);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("批量新增考试结果")
    @PostMapping("/addBatch")
//    @PreAuthorize("@ss.hasPermi('exam:info:list')")
    public CommonResult<Integer> addBatchExamResult(@RequestBody List<ExamResultInfoVO> list){
        int num = examResultInfoService.addBatchExamResult(list);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("查询考试信息")
    @PostMapping("/queryExam")
//    @PreAuthorize("@ss.hasPermi('exam:info:list')")
    public CommonResult<List<ExamInfoVO>> queryExamByName(@RequestBody ExamInfoVO vo){
        List<ExamInfoVO> list = examResultInfoService.queryExamByName(vo);
        return CommonResult.success(list);
    }
    
    @ApiOperation("查询补考名单")
    @PostMapping("/queryMakeUpExam")
    public CommonResult<List<BStudent>> queryMakeUpExam(@RequestBody ExamInfoVO vo) {
        List<BStudent> list = examResultInfoService.queryMakeUpExam(vo);
        return CommonResult.success(list);
    }

    @ApiOperation("查询班级学生列表")
    @PostMapping("/queryClassStudents")
//    @PreAuthorize("@ss.hasPermi('exam:info:list')")
    public CommonResult<List<BStudent>> queryClassStudents(@RequestBody ExamInfoVO vo){
        List<BStudent> list = examResultInfoService.queryClassStudents(vo);
        return CommonResult.success(list);
    }

    @ApiOperation("查询考试结果")
    @PostMapping("/queryExamResult")
//    @PreAuthorize("@ss.hasPermi('exam:info:list')")
    public CommonResult<List<ExamResultInfoVO>> queryExamResultList(@RequestBody ExamResultInfoVO vo){
        List<ExamResultInfoVO> list = examResultInfoService.queryExamResultList(vo);
        return CommonResult.success(list);
    }
    
    @ApiOperation("根据考试id查询考试详细信息")
    @GetMapping("/getExamDetail")
    public CommonResult<ExamInfoVO> getExamDetailById(@RequestParam Map<String, String> params) {
        ExamInfoVO examInfoVO = examResultInfoService.getExamDetailById(params);
        return CommonResult.success(examInfoVO);
    }
    
    @ApiOperation("根据考试id查找考试名单")
    @GetMapping("/getExamStudentsByExamId/{examId}")
    public CommonResult<List<BStudent>> getExamStudentsByExamId(@PathVariable String examId) {
        List<BStudent> examStudents = examResultInfoService.getExamStudentsByExamId(examId);
        return CommonResult.success(examStudents);
    }
    
    @ApiOperation("查看补考的学生名单和成绩")
    @GetMapping("/getResitExamStudents/{examId}")
    public CommonResult<List<ExamResultInfoVO>> getResitExamStudents(@PathVariable String examId) {
        List<ExamResultInfoVO> result = examResultInfoService.getResitExamStudents(examId);
        return CommonResult.success(result);
    }
}
